﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using Common;

namespace VMS_Web
{
    public partial class MasterItems : System.Web.UI.Page
    {
        private Master_Items_DB ObjMaster_Items__DB = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            h4title.InnerText = Request["Type"].ToString();

            if (!Page.IsPostBack)
            {
                if (Request["Mode"] == "Edit" && Request["Type"] != null && Request["Desc"] != null && Request["Value"]!=null)
                {
                  //  txtitemtype.Value = Request["Type"].ToString();
                    txtitemvalue.Value = Request["Value"].ToString();
                    txtitemdesc.Value = Request["Desc"].ToString();
                    btnAction.InnerText = "Edit";
                }

                else if (Request["Mode"] == "Delete" && Request["Type"] != null && Request["Value"] != null)
                {
                    //txtitemtype.Value = Request["Type"].ToString();
                    txtitemvalue.Value = Request["Value"].ToString();
                    btnAction.InnerText = "Delete";
                }
                else
                {
                    btnAction.InnerText = "Add";
                }

                if (Request["Mode"] == null)
                {
                    btnAction.InnerText = "Add";
                }
            }

            BindGrid(Request["Type"].ToString());

        }

        protected void btnAction_ServerClick(object sender, EventArgs e)
        {
            try
            {

                if (ObjMaster_Items__DB == null)
                {
                    ObjMaster_Items__DB = new Master_Items_DB();
                }

                //To Add new master type
                //For temparaty purpose user_id parameter is given as JitenP. Kindly replace with Session User_ID once login stuff is implemented
                if (Request["Mode"] == null)
                {
                    

                    MethodResult restult = ObjMaster_Items__DB.AddMasterItem(h4title.InnerText.Trim(),txtitemvalue.Value,txtitemdesc.Value, "JitenP");

                    MessageToDisplay(restult);

                }

                //To Edit existing master type

                if (Request["Mode"] == "Edit")
                {
                    

                   MethodResult restult = ObjMaster_Items__DB.UpdateMasterItem(h4title.InnerText.Trim(),txtitemvalue.Value,txtitemdesc.Value,true, "JitenP");

                    MessageToDisplay(restult);

                }
                //To Delete existing master type
                if (Request["Mode"] == "Delete")
                {

                    MethodResult restult = ObjMaster_Items__DB.DeleteMasterItem(h4title.InnerText.Trim(), txtitemvalue.Value,"JitenP");

                    MessageToDisplay(restult);

                }

                btnCancel_ServerClick(null, null);

            }

            catch (Exception exp)
            {
                //1- Log this exception using logger
                MessageToDisplay(MessageType.Error, "Oops!Some thing went wrong, contact application support team");

            }
        }

        //This procedure is to call when user clicks on Cancel button and after 
        //Add,Edit & Delete Procedures.

        protected void btnCancel_ServerClick(object sender, EventArgs e)
        {
            //txtitemtype.Value = "";
            txtitemvalue.Value = "";
            txtitemdesc.Value = "";

            btnAction.InnerText = "Add";

        }

        private void BindGrid(string master_item)
        {
            if (ObjMaster_Items__DB == null)
            {
                ObjMaster_Items__DB = new Master_Items_DB();
            }

            grdmasteritemdetails.DataSource = ObjMaster_Items__DB.GetMasterItems(master_item, true);
            grdmasteritemdetails.DataBind();

        }

        public string RedirectForEdit(string type,string value, string desc)
        {
            return "MasterItems.aspx?Mode=Edit&Type=" + type + "&Value=" + value +"&Desc=" + desc;

        }

        public string RedirectForDelete(string type, string value)
        {
            return "MasterItems.aspx?Mode=Delete&Type=" + type + "&Value=" + value;

        }

        private void MessageToDisplay(MethodResult methodResult)
        {
            if (MessageType.Error == methodResult.EnmMessageType)
            {
                diverror.Attributes["class"] = "alert alert-danger show";
                messagetodisplay.InnerText = methodResult.Message;

            }
            else if (MessageType.Success == methodResult.EnmMessageType)
            {
                diverror.Attributes["class"] = "alert alert-success show";
                messagetodisplay.InnerText = methodResult.Message;
            }
            else if (MessageType.Warning == methodResult.EnmMessageType)
            {
                diverror.Attributes["class"] = "alert alert-warning show";
                messagetodisplay.InnerText = methodResult.Message;
            }
            else if (MessageType.Information == methodResult.EnmMessageType)
            {
                diverror.Attributes["class"] = "alert alert-info show";
                messagetodisplay.InnerText = methodResult.Message;
            }
            else
            {
                diverror.Attributes["class"] = "hidden";
                messagetodisplay.InnerText = "";
            }
        }

        private void MessageToDisplay(MessageType messageType, string message)
        {
            if (MessageType.Error == messageType)
            {
                diverror.Attributes["class"] = "alert alert-danger show";
                messagetodisplay.InnerText = message;

            }
            else if (MessageType.Success == messageType)
            {
                diverror.Attributes["class"] = "alert alert-success show";
                messagetodisplay.InnerText = message;
            }
            else if (MessageType.Warning == messageType)
            {
                diverror.Attributes["class"] = "alert alert-warning show";
                messagetodisplay.InnerText = message;
            }
            else if (MessageType.Information == messageType)
            {
                diverror.Attributes["class"] = "alert alert-info show";
                messagetodisplay.InnerText = message;
            }
            else
            {
                diverror.Attributes["class"] = "hidden";
                messagetodisplay.InnerText = "";
            }
        }
    }
}